<div class="wmm-module">
    <div class="layui-form-item">
        <form class="layui-form"  action="javascript:;">
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="requests.init()">一键初始化</button>
            </div>    
            <div class="layui-inline">    
                <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="renders.del_box()">一键删除</button>
            </div>        
            <div class="layui-inline" wmm-fb-auth="DEVELOPER">
                <div class="layui-input-inline">
                    <select name="paramId">
                        <option value="">选择初始化参数</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" wmm-fb-auth="DEVELOPER">
                <div class="layui-input-inline">
                    <select name="templateGroupId">
                        <option value="">选择初始化模板组</option>
                    </select>
                </div>
            </div>
            <button lay-submit lay-filter="generate" class="layui-btn layui-btn-normal layui-btn-sm">一键生成</button>
        </form>
    </div>

    <div class="layui-form-item">
            
        <form id="search-form" class="layui-form" action="javascript:;">
            
            <div class="layui-inline">
                <input type="text" name="tableName" autocomplete="off" lay-verify="required" placeholder="表名"
                    class="layui-input">
            </div>
            <div class="layui-inline">
                <input type="text" name="className" autocomplete="off" lay-verify="required" placeholder="类名"
                    class="layui-input">
            </div>

            <div class="layui-inline">
                <button type="submit"  class="layui-btn layui-btn-normal layui-btn-sm datatable-search"><i class="layui-icon layui-icon-search search-icon"></i>
                    查询</button>
            </div>
        </form>
    </div>

    <div>
        <div id="tb-sys-table-head" lay-filter="tb-sys-table-head">
        </div>
    </div>
</div>


<template id="tmp-resource-form">
    <div style="padding:10px">
        <form class="layui-form" lay-filter="resource-form" action="javascript:;">
            <input type="hidden" value="" name="id" />

            <div class="layui-form-item">
                <label class="layui-form-label">所属应用<span class="required-star">*</span></label>
                <div class="layui-inline" style="width:300px">
                    <select name="appsId" xm-select="appsIdBox" xm-select-skin="normal">
                        <option value="">所有应用</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">资源名称<span class="required-star">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="resourceName" autocomplete="off" lay-verify="required" placeholder="请输入资源名称"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">资源编码</label>
                <div class="layui-input-block">
                    <input type="text" name="resourceCode" placeholder="请输入资源编码" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">资源路径<span class="required-star">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="url" lay-verify="required" placeholder="请填写接口路径，(资源的路径一般为名词)" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn lay-submit layui-btn-normal" lay-submit lay-filter="resource">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</template>



<script>

    new Wmm({
        vars: {
            table: {},
            appList: undefined,
            reqUrl: "/sysbeetletablehead/",
            templateGroupUrl: "/sysbeetletemplategroup/all",
            paramUrl: "/sysbeetleparams/all",
            generateUrl: "/generate/code"
        },

        imports: ["form","api","datatable","req","sysTools"],
        listener: function () {

            fb.form.on('submit(resource)', function (data) {
                var dataObject = data.field;
                dataObject.appsId = fb.formSelects.value("appsIdBox","val");
                if (dataObject.id) {
                    //update
                    fb.api.resource.update(dataObject, function () {
                        layer.closeAll();
                        layer.msg("改好了");
                        vars.table.flush(true);
                    });
                    return;
                }
                //insert
                fb.api.resource.add(dataObject, function () {
                    layer.msg("加好了...你继续");
                    vars.table.flush(true);
                });
            });
        },
        requests: {
            init:function(){
                req.get(vars.reqUrl+"init",function(data){
                    console.log("======进入成功======");
                    console.log("data:"+JSON.stringify(data));
                    if(data.code == 0){
                        layer.msg("初始化成功",{icon:6});
                        vars.table.flush();
                    }
                })
            }
        },
        listener: function(){
          fb.form.on("submit(generate)",function(data){
              console.log(data);
              var generation = data.field;
              var tableIds = vars.table.getCheckedData();
              generation.tableIds = tableIds;
              console.log("======>"+JSON.stringify(generation))
              req.post(vars.generateUrl,generation,function(data){
                  if(typeof(data.code) == 'undefined'){
                    layer.msg("一键生成成功",{icon:6});
                  }
                  console.log("=============成功==============");
              },function(ret){
                  console.error("============失败================")
              })
              return false;
              // req.get()
          })
        },
        renders: {
            // box: function (data, isCreate) {
            //     layer.open({
            //         type: 1,
            //         title: (isCreate ? "添加" : "修改") + "资源",
            //         content: document.querySelector("#tmp-resource-form").innerHTML,
            //         area: ['500px', '330px'],
            //         success: function () {
            //             sysTools.renderAppSelect("appsIdBox");
            //             if (!isCreate) {
            //                 //表单初始赋值
            //                 fb.form.val('resource-form', data)
            //                 fb.formSelects.value("appsIdBox", data.appsId);
            //             }
            //             fb.form.render();
            //         }
            //     });
            // },
            del_box: function(){
                var tables = vars.table.getCheckedData();
                var flag = false;
                if(tables.length == 0){
                    flag = true;
                    layer.msg("请选择复选框？",{icon:5});
                }
                if(flag){
                    return;
                }

                layer.confirm("是否一键删除复选框选中的数据?",function(index){
                    var tableIds = '';
                    console.log(tables);
                    for(var i =0;i<tables.length;i++){
                        tableIds += tables[i].id +",";
                    }
                    tableIds = tableIds.substr(0,tableIds.length-1);
                    console.log("=========:"+tableIds);
                    req.get(vars.reqUrl+"batchDelete?tablesId="+tableIds,function(data){
                        console.log("======进入成功======");
                        console.log("data:"+JSON.stringify(data));
                        if(data.code == 0){
                            layer.msg("一键删除成功",{icon:6});
                            vars.table.flush();
                        }
                    })

                    layer.close(index);
                })

            },
            render: function () {

                req.get(vars.templateGroupUrl,function(data){
                    fb.utils.render.select("[name=templateGroupId]",data,"groupName","id");
                })

                req.get(vars.paramUrl,function(data){
                    fb.utils.render.select("[name=paramId]",data,"alias","id");
                })

                vars.table = new DataTable({
                    el: "tb-sys-table-head",
                    formEl: "search-form",
                    url: vars.reqUrl,
                    multiple:true,
                    firstQueryData: function () {  //
                        var params = fb.router.state;
                        fb.form.val("search-form", params);
                        if (params.appsId) {
                            if (typeof params.appsId === "number") {
                                var arr = [];
                                arr.push(params.appsId)
                                return params;
                            }
                            fb.formSelects.value("appsId", params.appsId.split(","));
                        }
                        return params;
                    },
                    fixHeightNumber: 300,
                    defines: [
                        {
                            head: "id",
                            prop: "id",
                        }, {
                            head: "表名",
                            prop: "tableName",
                        }, {
                            head: "类名",
                            prop: "className",
                        },
                        {
                            head: "功能(表备注)",
                            prop: "content",
                            width:200
                        },
                        {   head: "是否跳转<增加/修改/详情>",
                            prop: "forward",
                            type: "checkbox",
                            change: function (filterData, data) {
                                var dom = filterData.elem;
                                var dataObj = {}
                                dataObj.id = data.id;
                                dataObj.forward = dom.checked;
                                console.log(dataObj);
                                req.put(vars.reqUrl,dataObj,function(ret){
                                    layer.msg("成功");
                                    vars.table.flush();
                                });
                            }
                        },
                        {
                            head: "创建时间",
                            prop: "gmt_create",
                            type: "datetime"
                        }
                    ],
                    handles: [
                        {
                            edit: function (data) {
                                router.route({url: "/fastboot/pages/generator/tablefield",pushState: {tablehead: data, searchForm: vars.table.getSearchForm()}
                                });
                            },
                            name: "表管理",
                            color: "#a76d5a",
                            visible: function () {
                                return AppConfig.authorization.resourcesHandlerEnabled || false;
                            }
                        },
                        {
                            del: {
                                tip: "确定要删除吗？",
                                do: function (data) {
                                    req.delete(vars.reqUrl+"using/"+data.id,function(data){
                                        console.log("======进入成功======");
                                        console.log("data:"+JSON.stringify(data));
                                        if(data.code == 0){
                                            layer.msg("删除成功",{icon:6});
                                            vars.table.flush();
                                        }
                                    })
                                }
                            }
                        },

                    ],
                });

            },
        },

    });



</script>